<template>
  <div class="map">
    <div class="maxWidth">
      <div class="mapOut">
        <div id="container"></div>
        <div @click="lookInfo" class="infoBtn" v-show="infoListShow">
          &nbsp;&nbsp;&nbsp;&nbsp;巡检信息
        </div>
        <div @click="fullScreen" class="fullScreen">
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;全屏[X]
        </div>
        <div v-show="!infoListShow" class="infoList">
          <div @click="closeInfoList" class="close">
            <span>巡检信息</span>
            <i class="el-icon-close"></i>
          </div>
          <el-tree
            :data="data"
            :props="defaultProps"
            @node-click="handleNodeClick"
          ></el-tree>
        </div>
        <div class="bt">
          <div class="btn" @click="addFillLayer">一级片区</div>
          <div class="btn" @click="addFillLayerII">二级片区</div>
          <div class="btn" @click="getP3Window">显示设备点</div>
        </div>
      </div>
    </div>
    <div class="gisMapRight">
      <div class="topCards">
        <div class="topcard" id="topcard1"></div>
        <div class="topcard" id="topcard2"></div>
      </div>
      <div class="bottomList" id="">
        <ul class="listHeader">
          <li class="name">巡检人</li>
          <li class="number">巡检组别</li>
          <li class="pos">巡检位置</li>
          <li class="time">巡检时间</li>
          <li class="lookBtn">巡检轨迹</li>
        </ul>
        <div class="myline"></div>
        <ul v-for="item in xunjianList" :key="item" class="listCon">
          <li class="name">李刚</li>
          <li class="number">一号管线巡检</li>
          <li class="pos">西安市雁塔区丈八六路</li>
          <li class="time">2023.03.21 10:21</li>
          <li class="lookBtn"><el-button size="mini">查看轨迹</el-button></li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import pianqu from "@/assets/json/pianqu.json";
import { mapActions, mapState } from "vuex";

export default {
  name: "LocusManage",
  data() {
    return {
      fillLayer: null,
      infoListShow: true,
      zoom: 0,
      mapDom: {},
      map: {},
      startWidth: 0,
      WidthMin: 0,
      WidthMax: 0,
      WidthEms:49.5,
      timer: null,
      isFull: false,
      xunjianList: [1, 2, 3, 4, 5],
      data: [
        {
          label: "一级 1",
          children: [
            {
              label: "二级 1-1",
              children: [
                {
                  label: "三级 1-1-1",
                },
              ],
            },
            {
              label: "一级 2",
              children: [
                {
                  label: "二级 2-1",
                  children: [
                    {
                      label: "三级 2-1-1",
                    },
                  ],
                },
                {
                  label: "二级 2-2",
                  children: [
                    {
                      label: "三级 2-2-1",
                    },
                  ],
                },
              ],
            },
            {
              label: "一级 3",
              children: [
                {
                  label: "二级 3-1",
                  children: [
                    {
                      label: "三级 3-1-1",
                    },
                  ],
                },
                {
                  label: "二级 3-2",
                  children: [
                    {
                      label: "三级 3-2-1",
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
      point: null,
      poinAndFill: [
        {
          posi: {
            x: 114.65379068334498,
            y: 35.22374793559449,
          },
          name: "片区01",
        },
        {
          posi: {
            x: 114.67587620434227,
            y: 35.222776309815444,
          },
          name: "片区02",
        },
        {
          posi: {
            x: 114.68996317643187,
            y: 35.221570995635375,
          },
          name: "片区03",
        },
        {
          posi: {
            x: 114.70312161319781,
            y: 35.22136487037481,
          },
          name: "片区04",
        },
        {
          posi: {
            x: 114.71717231905104,
            y: 35.219818352274274,
          },
          name: "片区05",
        },
        {
          posi: {
            x: 114.6896416975509,
            y: 35.21397557178534,
          },
          name: "片区06",
        },
        {
          posi: {
            x: 114.70094901742365,
            y: 35.21353226970217,
          },
          name: "片区07",
        },
        {
          posi: {
            x: 114.71401225495302,
            y: 35.211532031906344,
          },
          name: "片区08",
        },
        {
          posi: {
            x: 114.64716998721849,
            y: 35.211324779469855,
          },
          name: "片区09",
        },
        {
          posi: {
            x: 114.67827532171087,
            y: 35.20852824066002,
          },
          name: "片区10",
        },
        {
          posi: {
            x: 114.68869200431376,
            y: 35.20781676249812,
          },
          name: "片区11",
        },
        {
          posi: {
            x: 114.69859243376987,
            y: 35.20796441297072,
          },
          name: "片区12",
        },
        {
          posi: {
            x: 114.70937846557926,
            y: 35.20489288683186,
          },
          name: "片区13",
        },
        {
          posi: {
            x: 114.6403130219933,
            y: 35.2020792536307,
          },
          name: "片区14",
        },
        {
          posi: {
            x: 114.66487325601022,
            y: 35.19971964538077,
          },
          name: "片区15",
        },
        {
          posi: {
            x: 114.67499949758474,
            y: 35.19841000117788,
          },
          name: "片区16",
        },
        {
          posi: {
            x: 114.68739174334777,
            y: 35.197391534509464,
          },
          name: "片区17",
        },
        {
          posi: {
            x: 114.70381328367229,
            y: 35.19545054201792,
          },
          name: "片区18",
        },
        {
          posi: {
            x: 114.72797042710071,
            y: 35.19533065407859,
          },
          name: "片区19",
        },
        {
          posi: {
            x: 114.63083722729078,
            y: 35.18844661563833,
          },
          name: "片区20",
        },
        {
          posi: {
            x: 114.67296484625315,
            y: 35.18388082357119,
          },
          name: "片区21",
        },
        {
          posi: {
            x: 114.68456804632702,
            y: 35.18308933855066,
          },
          name: "片区22",
        },
        {
          posi: {
            x: 114.69585852760027,
            y: 35.182706854069856,
          },
          name: "片区23",
        },
        {
          posi: {
            x: 114.71244937746863,
            y: 35.18286790383879,
          },
          name: "片区24",
        },
        {
          posi: {
            x: 114.70545166980142,
            y: 35.15951810560744,
          },
          name: "片区25",
        },
        {
          posi: {
            x: 114.65494705729381,
            y: 35.16601176249328,
          },
          name: "片区26",
        },
        {
          posi: {
            x: 114.6975676187813,
            y: 35.13604490586162,
          },
          name: "片区27",
        },
      ],
    };
  },
  methods: {
    ...mapActions("gismanage", ["actionGetDevPList"]),
    // 全屏事件
    fullScreen() {
      let menu = document.querySelector(".map");
      let mapOut = document.querySelector(".mapOut");
      this.WidthMax = menu.offsetWidth;
      console.dir("最大", mapOut);
      if (!this.isFull) {
        this.mapDom = document.getElementById("container");
        this.startWidth = this.mapDom.offsetWidth;
        //清除定时器
        clearInterval(this.timer);
        // 不是全屏变全屏
        this.timer = setInterval(() => {
          this.addOrEditWid(mapOut, this.startWidth, this.WidthMax, 20);
          // this.map.centerAndZoom(this.point, 13);
          // console.log(1);
        }, 30);
        this.map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
        this.isFull = !this.isFull;
      } else {
        //清除定时器
        clearInterval(this.timer);
        // 全屏变半屏
        this.timer = setInterval(() => {
          this.addOrEditWid(mapOut, this.startWidth, this.WidthMax, -20);
          // this.map.centerAndZoom(this.point, 13);
          // console.log(1);
        }, 30);
        this.map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
        this.isFull = !this.isFull;
      }
    },
    addOrEditWid(dom, iWidthMin, iWidthMax, iSpeed) {
      dom.style.display = "block";
      if (iSpeed > 0) {
        // 变宽
        if (this.WidthEms < 100) {
          //dom.style.width = dom.offsetWidth + iSpeed + "px";

           this.WidthEms = this.WidthEms + 0.5;
           dom.style.width = this.WidthEms + "%" ;
           
        }
      } else {
        if (this.WidthEms > 49.5) {
          //dom.style.width = dom.offsetWidth + iSpeed + "px";
          this.WidthEms = this.WidthEms - 0.5;
          dom.style.width = this.WidthEms + "%" ;

        }
      }
    },
    // addOrEditWid(dom, iWidthMin, iWidthMax, iSpeed) {
    //   dom.style.display = "block";
    //   if (iSpeed > 0) {
    //     // 变宽
    //     if (dom.offsetWidth < iWidthMax) {
    //       dom.style.width = dom.offsetWidth + iSpeed + "px";
    //     }
    //   } else {
    //     if (dom.offsetWidth > iWidthMin) {
    //       dom.style.width = dom.offsetWidth + iSpeed + "px";
    //     }
    //   }
    // },
    // 查看巡检信息事件
    lookInfo() {
      this.infoListShow = false;
    },
    closeInfoList() {
      this.infoListShow = true;
    },
    // 图层相关
    addFillLayer() {
      this.removeFillLayer();
      fetch("yang/area/coordinate")
        .then((res) => {
          return res.json();
        })
        .then((testFillData) => {
          if (!this.fillLayer) {
            this.fillLayer = new BMapGL.FillLayer({
              crs: "GCJ02",
              enablePicked: true,
              autoSelect: true,
              pickWidth: 30,
              pickHeight: 30,
              selectedColor: "red", // 悬浮选中项颜色
              border: true,
              style: {
                // fillColor: [
                //   "case",
                //   ["boolean", ["feature-state", "picked"], false],
                //   "red",
                //   [
                //     "match",
                //     ["get", "name"],
                //     "海淀区",
                //     "#ce4848",
                //     "朝阳区",
                //     "blue",
                //     "通州区",
                //     "blue",
                //     "丰台区",
                //     "#6704ff",
                //     "房山区",
                //     "#6704ff",
                //     "orange",
                //   ],
                // ],
                fillOpacity: 0.3,
                strokeWeight: 2,
                strokeColor: "blue",
                fillColor: "green",
              },
            });
            this.fillLayer.addEventListener("click", function (e) {
              if (e.value.dataIndex !== -1 && e.value.dataItem) {
                console.log("click", e.value.dataItem);
                // 使用样式配置，实现单选或多选效果
                // this.updateState(e.value.dataIndex, { picked: true }, true);
              }
            });
          }
          console.log("片区 数据", testFillData);
          this.map.addNormalLayer(this.fillLayer);
          this.fillLayer.setData(testFillData.data);
        });
    },
    addFillLayerII() {
      this.removeFillLayer();
      fetch("yang/area/coordinate_out")
        .then((res) => {
          return res.json();
        })
        .then((testFillData) => {
          if (!this.fillLayer) {
            this.fillLayer = new BMapGL.FillLayer({
              crs: "GCJ02",
              enablePicked: true,
              autoSelect: true,
              pickWidth: 30,
              pickHeight: 30,
              selectedColor: "red", // 悬浮选中项颜色
              border: true,
              style: {
                fillOpacity: 0.3,
                strokeWeight: 2,
                strokeColor: "blue",
                fillColor: "green",
              },
            });
            this.fillLayer.addEventListener("click", function (e) {
              if (e.value.dataIndex !== -1 && e.value.dataItem) {
                console.log("click", e.value.dataItem);
                // 使用样式配置，实现单选或多选效果
                // this.updateState(e.value.dataIndex, { picked: true }, true);
              }
            });
          }
          console.log("片区 数据", testFillData);
          this.map.addNormalLayer(this.fillLayer);
          let warrData = {
            features: [],
            type: "FeatureCollection",
          };
          warrData.features.push(testFillData.data.features[26]);
          console.log("testFillData", testFillData.data.features[26]);
          this.fillLayer.setData(testFillData.data);
          // this.fillLayer.setData(warrData);
        });
    },
    // 片区信息展示
    shouFillInfo() {
      this.poinAndFill.forEach((item) => {
        let opts = {
          position: new BMapGL.Point(item.posi.x, item.posi.y), // 指定文本标注所在的地理位置
          offset: new BMapGL.Size(-15, -15), // 设置文本偏移量
        };
        let label = new BMapGL.Label(item.name, opts);
        // 自定义文本标注样式
        label.setStyle({
          color: "blue",
          borderRadius: "5px",
          borderColor: "#ccc",
          padding: "px",
          fontSize: "12px",
          height: "15px",
          lineHeight: "15px",
          fontFamily: "微软雅黑",
        });
        this.map.addOverlay(label);
      });
      // 创建文本标注对象
    },
    // 移除图层
    removeFillLayer() {
      this.map.removeNormalLayer(this.fillLayer);
    },
    //  巡检点击事件
    handleNodeClick(data) {
      console.log(data);
    },
    getTopcard1() {
      let topcard1 = document.getElementById("topcard1");
      let myChart = this.$echarts.init(topcard1);
      let option = {
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        title: {
          text: "设备异常情况",
          padding: [13, 0, 0, 10],
          textStyle: {
            color: "#fff",
            fontWeight: "normal",
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
          
        },
        legend: {
          data: ["正常", "故障"],
          top: 13,
          right: 13,
          textStyle: {
            color: "#fff",
          },
        },
        // 下载等图标
        // toolbox: {
        //   show: true,
        //   orient: "vertical",
        //   left: "right",
        //   top: "center",
        //   feature: {
        //     mark: { show: true },
        //     dataView: { show: true, readOnly: false },
        //     magicType: { show: true, type: ["line", "bar", "stack"] },
        //     restore: { show: true },
        //     saveAsImage: { show: true },
        //   },
        // },
        xAxis: [
          {
            type: "category",
            axisTick: { show: false },
            data: ["设备异常", "设备告警", "故障缺陷"],
            axisLabel: {
              textStyle:{
                color:'#fff',  //坐标的字体颜色
              },
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            splitLine: {
              show: false,
            },
            axisLabel: {
              textStyle:{
                color:'#fff',  //坐标的字体颜色
              },
            },
          },
        ],
        series: [
          {
            name: "正常",
            type: "bar",
            barGap: 0,
            emphasis: {
              focus: "series",
            },
            data: [320, 332, 301],
            itemStyle: {
              normal: {
                //这里是重点
                color: "#00FF5E",
              },
            },
          },
          {
            name: "故障",
            type: "bar",
            emphasis: {
              focus: "series",
            },
            data: [220, 182, 191],
            itemStyle: {
              normal: {
                //这里是重点
                color: "#FA5151",
              },
            },
          },
        ],
      };
      myChart.setOption(option);
    },
    getTopcard2() {
      let topcard2 = document.getElementById("topcard2");
      let myChart = this.$echarts.init(topcard2);
      let option = {
        title: {
          text: "设备类型占比",
          padding: [13, 0, 0, 10],
          textStyle: {
            color: "#fff",
            fontWeight: "normal",
          },
        },
        legend: {
          top: "bottom",
          textStyle: {
            color: "#fff",
          },
        },
        // toolbox: {
        //   show: true,
        //   feature: {
        //     mark: { show: true },
        //     dataView: { show: true, readOnly: false },
        //     restore: { show: true },
        //     saveAsImage: { show: true },
        //   },
        // },
        series: [
          {
            name: "Nightingale Chart",
            type: "pie",
            radius: [20, 80],
            center: ["50%", "50%"],
            roseType: "area",
            itemStyle: {
              borderRadius: 8,
            },
            data: [
              { value: 40, name: "管线" },
              { value: 38, name: "阀门" },
              { value: 32, name: "水泵" },
              { value: 30, name: "水表" },
              { value: 28, name: "水源" },
              { value: 26, name: "其他" },
            ],
          },
        ],
      };
      myChart.setOption(option);
    },
    getP3Window() {
      console.log("devPList", this.devPList.result);
      this.devPList.result.forEach((item) => {
        let marker = new BMapGL.Marker(new BMapGL.Point(item.cx, item.cy));
        marker.cdevice = item.cdevice;
        marker.addEventListener("click", () => {
          alert("cdevice " + marker.cdevice);
          // console.log(marker);
        });
        this.map.addOverlay(marker);
      });
      // var marker = new BMapGL.Marker(new BMapGL.Point(108.934679, 34.210952));
      // marker.Eposition = {x:108.934679, y:34.210952}
      // this.map.addOverlay(marker);
      // marker.addEventListener('click',()=>{
      //   alert('x'+marker.Eposition.x+'----y:'+marker.Eposition.y)
      //   console.log(marker)
      // })
      // var sContent =
      // `
      //   <div style='width:190px;text-align: center;margin-top: 0px;'>天安门</div>
      //   <div style='color:white'><span>设备名称:<span/><span>管线<span/></div>
      //   <div style='color:white'><span>设备位置:<span/><span>雁塔区大寨路<span/></div>
      //   <div style='color:white'><span>所属线路:<span/><span>管线运营一号线<span/></div>
      //   <div style='color:white'><span>故障率:<span/><span>20%<span/></div>
      //   <div style='color:white'><span>数据更新时间:<span/><span>2023-03-21 11:50<span/></div>
      //   <div style='width:190px;height:120px'><img id='imgDemo' style='height:100%;width:100%' src="./assets/R-C.png" alt=""></div>
      // `;
      // var infoWindow = new BMapGL.InfoWindow(sContent);
      // marker.addEventListener('click',function(){
      //   this.openInfoWindow(infoWindow)
      //   var dom = document.getElementById('imgDemo')
      //   console.log('dom',dom)
      //   dom.onload = function(){
      //     infoWindow.redraw()
      //   }
      // })
    },
  },
  mounted() {
    (this.map = new BMapGL.Map("container")), console.log(this.map);
    this.point = new BMapGL.Point(114.673864, 35.183726);
    this.actionGetDevPList();
    // 114.673864,35.183726
    this.map.centerAndZoom(this.point, 13);
    this.map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
    // 地图添加点击事件
    // this.map.addEventListener("click", function (e) {
    //   console.log("点击位置经纬度：" + e.latlng.lng + "," + e.latlng.lat);
    // });
    // this.addFillLayer();
    // this.addFillLayerII()
    // this.getP3Window();
    this.getTopcard1();
    this.getTopcard2();
    this.shouFillInfo();
  },
  computed: {
    ...mapState("gismanage", ["devPList"]),
  },
};
</script>

<style lang="less" scoped>
.map {
  height: 100%;
  width: 100%;
  border-radius: 30px;
  overflow: hidden;
  position: relative;

  .maxWidth {
    width: 100%;
    height: 100%;
    .mapOut {
      position: relative;
      width: 49.5%;
      height: 100%;
      z-index: 999;
      #container {
        border-radius: 30px;
        height: 100%;
        width: 100%;
      }
      .infoBtn {
        position: absolute;
        width: 100px;
        height: 45px;
        background-color: rgba(16, 3, 3, 0.5);
        top: 0;
        left: 0;
        cursor: pointer;
        z-index: 99;
        font-size: 16px;
        color: white;
        line-height: 45px;
      }
      .fullScreen {
        position: absolute;
        width: 100px;
        height: 45px;
        background-color: rgba(16, 3, 3, 0.5);
        top: 0;
        right: 0;
        cursor: pointer;
        z-index: 99;
        font-size: 16px;
        color: white;
        line-height: 45px;
        border-top-right-radius: 30px;
      }
      .bt {
        display: flex;
        flex-direction: column;
        width: 80px;
        padding: 5px;
        // height: 40px;
        cursor: pointer;
        position: absolute;
        top: 60px;
        right: 0;
        .btn {
          z-index: 99;
          width: 80px;
          height: 30px;
          background-color: rgba(16, 3, 3, 0.5);
          border-radius: 5px;
          line-height: 30px;
          text-align: center;
          margin-top: 3px;
        }
      }
      .infoList {
        width: 280px;
        height: 100%;
        background-color: rgba(16, 3, 3, 0.5);
        position: absolute;
        top: 0;
        left: 0;
        border-radius: 30px;
        z-index: 99;
        .close {
          margin-left: 15px;
          margin-top: 10px;
          cursor: pointer;
          ::v-deep [class*=" el-icon-"],
          [class^="el-icon-"] {
            color: white;
          }
          span {
            font-size: 24px;
            color: white;
          }
          i {
            margin-left: 135px;
          }
        }
        .el-tree {
          background-color: transparent;
          color: white;
        }
        ::v-deep .el-tree-node__content:hover {
          background-color: transparent;
          color: white;
        }
        ::v-deep .el-tree-node:focus > .el-tree-node__content {
          background-color: transparent;
          color: white;
        }
        ::v-deep .el-tree-node__content:hover {
          background-color: transparent;
          color: white;
        }
      }
    }
  }

  .gisMapRight {
    position: absolute;
    top: 0;
    right: 0;
    width: 49.5%;
    height: 100%;
    border-radius: 30px;
    background-color: #0c323a;
    .topCards {
      width: 100%;
      height: 255px;
      display: flex;
      justify-content: space-between;
      .topcard {
        width: 49%;
        height: 100%;
        border-radius: 30px;
        background-image: url(../../../assets/bcp1.png);
        background-size: 100% 100%;
      }
    }
    .bottomList {
      width: 100%;
      height: calc(100% - 275px);
      background-color: #002127;
      margin-top: 20px;
      border-radius: 30px;
      color: white;
      .listHeader {
        box-sizing: border-box;
        padding-left: 20px;
        padding-right: 20px;
        padding-top: 30px;
        padding-bottom: 30px;
        // padding: 30 , 20 , 30 , 20;
        width: 100%;
        display: flex;
        justify-content: space-between;
        text-align: center;
        height: 32px;
        font-size: 20px;
        li {
          list-style: none;
        }
        .name {
          width: 60px;
        }
        .number {
          width: 100px;
        }
        .pos {
          width: 200px;
        }
        .time {
          width: 150px;
        }
        .lookBtn {
          width: 130px;
        }
      }
      .myline {
        width: 98%;
        margin: auto;
        height: 1px;
        background-color: #0c323a;
      }
      .listCon {
        box-sizing: border-box;
        padding-left: 20px;
        padding-right: 20px;
        width: 100%;
        height: 32px;
        display: flex;
        text-align: center;
        justify-content: space-between;
        li {
          list-style: none;
        }
        .name {
          width: 60px;
        }
        .number {
          width: 100px;
        }
        .pos {
          width: 200px;
        }
        .time {
          width: 150px;
        }
        .lookBtn {
          width: 130px;
          .el-button {
            background-color: #00b8b8;
            color: white;
            border: none;
          }
        }
      }
    }
  }
}
</style>